<?php
/*
 * 用户控制器
 */
header("Content-Type: text/html;charset=UTF-8");
class User extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
		$this->load->library ( 'pagination' );
		$this->load->library('encrypt'); /*加密类*/
	}

	public function index($offset = '') {
		$user_name=$this->session->userdata('username');
		if($user_name){
			$this->load->model ( 'user_model', 'usermodel' );
			$limit = 10;
			$total = $this->usermodel->count_nums ();
			$data['total']=$total;
			$data['offset']=$offset;
			$data ['user'] = $this->usermodel->list_users ( $limit, $offset );
			$current_total=count($data ['user']);
			$data['current_total']=$offset+$current_total;
			
			$config ['base_url'] = base_url () . 'index.php/user/index/';
			$config ['total_rows'] = $total;
			$config ['per_page'] = $limit;
			$config ['num_links'] = 4;
			$config ['first_link'] = '首页';
			$config ['last_link'] = '尾页';
			$this->pagination->initialize ( $config );
			$data ['pag_links'] = $this->pagination->create_links ();
			
			$data ['page_title'] = '用户 — 昊美科技CRM系统';
			$data ['class6'] = 'cur';
			$this->load->view ( 'user_list', $data );
		}else{
			$this->load->view ( 'login');
		}
	}

	/*
	 * 客户名查询记录
	*/
	public function check($name = ''){
		$this->db->like('user_name', urldecode($name));
		$query = $this->db->get('user');
		$data['user'] = $query->result();
		$data ['pag_links'] = '';
		$data ['page_title'] = '用户 — 昊美科技CRM系统';
		$data ['class6'] = 'cur';
		$this->load->view ( 'user_list', $data );
	}

	/*
	 * 删除记录
	*/
	public function delete($id = '') {
		$this->db->delete ( 'user', array ('id' => $id ) );
		redirect ( 'user/index/' );
	}

	/*
	 * 查看某条记录
	*/
	public function find($id = '') {
		$query = $this->db->get_where ( 'user', array ('id' => $id ) );
		$find_data['find_user'] = $query->row();
		$this->load->view ( 'user_view', $find_data );
	}

	/*
	 * 查看某条编辑记录
	*/
	public function update($id = '') {
		$query = $this->db->get_where ( 'user', array ('id' => $id ) );
		$update_data['update_user'] = $query->row();
		$update_data['method'] = 'user';
		$this->load->view ( 'user_edit', $update_data );
	}

	/*
	 * 编辑某条纪律
	*/
	public function update_edit() {
		$data_array = $_POST; /*接受参数数组*/
		$id = $data_array['id'];
		$encrypted_string = sha1($data_array['password']); /*加密后的密码*/
		$data_array['password'] = $encrypted_string;
		unset($data_array['id']);
		$this->db->where('id', $id);
		$this->db->update('user', $data_array);
		$row=$this->db->affected_rows();
		if($row > 0){
			redirect ( 'user/index/' );
		}else{
		}
	}

	/*
	 * 新增客户
	*/
	public function show_customer_add(){ /*显示增加页面*/
		$this->load->view ( 'user_add');
	}

	public function insert(){
		$data_array = $_POST;
		$time=date('Y-m-d H:i:s', strtotime('now'));
		$data_array['create_time']=$time;
		$data_array['last_region_time']=$time;
		$encrypted_string = sha1($data_array['password']);
		$data_array['password'] = $encrypted_string;
		$this->db->insert('user', $data_array);
		$row=$this->db->affected_rows();
		if($row > 0){
			redirect ( 'user/index/' );
		}else{

		}
	}
	
	/*检测旧密码*/
	public function check_ago_password(){
		$data_array = $_POST;
		$user_id=$this->session->userdata('id');
		$encrypted_string = sha1($data_array['pswd']);
		$query = $this->db->query("SELECT count(*) as nums FROM user where id = $user_id and password = '".$encrypted_string."'");
		$row = $query->row();
		if($row->nums == 0){
			echo '';
		}else{
			echo $row->nums;
		}
	}
	
	/*修改密码*/
	public function modify_pswd(){
		$data_array = $_POST;
		$encrypted_string = sha1($data_array['pswd1']);
		$data_array['password'] = $encrypted_string;
		unset($data_array['pswd']);
		unset($data_array['pswd1']);
		$time=date('Y-m-d H:i:s', strtotime('now'));
		$data_array['update_time'] = $time;
		
		$this->db->where('id', $user_id=$this->session->userdata('id'));
		$row = $this->db->update('user', $data_array);
		$this->load->view ( 'login',$data);
	}
}
?>